
import os
import sys

from distutils.core import setup
from distutils.errors import DistutilsFileError
from distutils.command.install_lib import install_lib
from distutils.command.install_data import install_data


if sys.version_info < (2, 5):
    sys.exit("requires python 2.5 and above")

#===============================================================================
# Globals
#===============================================================================
lib_dir = ""
data_dir_defined = False

for name in sys.argv[1:]:
    if name.startswith("--install-data"):
        data_dir_defined = True

#===============================================================================
# Custom Command Class
#===============================================================================
class my_install_lib(install_lib):
    def _create_pth_file(self, file_path):
        f = open(file_path, "w")
        f.write("dexen-0.3.2\n")
        f.close()
        
    def run(self):
        global lib_dir
        file_path = os.path.join(self.install_dir, "dexen.pth")
        self.install_dir = os.path.join(self.install_dir, "dexen-0.3.2")
        lib_dir = self.install_dir
        install_lib.run(self)
        self._create_pth_file(file_path)
        

class my_install_data(install_data):
    def copy_tree_without_svn (self, src, dst):       
        if not os.path.isdir(src):
            raise DistutilsFileError, \
                  "cannot copy tree '%s': not a directory" % src
        try:
            names = os.listdir(src)
        except os.error, (_, errstr):
            raise DistutilsFileError, \
                  "error listing files in '%s': %s" % (src, errstr)
    
        self.mkpath(dst)
    
        outputs = []
    
        for n in names:
            src_name = os.path.join(src, n)
            dst_name = os.path.join(dst, n)
       
            if os.path.isdir(src_name):
                if not src_name.endswith(".svn"):
                    outputs.extend(self.copy_tree_without_svn(src_name, dst_name))
            else:
                self.copy_file(src_name, dst_name)
                outputs.append(dst_name)
    
        return outputs    
    
    def run(self):
        if not data_dir_defined:
            self.install_dir = lib_dir
        #install_data.run(self)
        self.copy_tree_without_svn("examples", 
                                   os.path.join(self.install_dir, "examples"))
        self.copy_file("README.txt", self.install_dir)
        self.copy_file("LICENSE.txt", self.install_dir)

#===============================================================================
# 
#===============================================================================
_cmdclass = {
    "install_data" : my_install_data,
    "install_lib" : my_install_lib
}

_scripts = [
    os.path.join('dexen', 'scripts', 'dexen-cli.py'),
    os.path.join('dexen', 'scripts', 'dexen-node.py'),
    os.path.join('dexen', 'scripts', 'dexen-server.py')
]

_dexen_pkgs = [
    'dexen',
    'dexen.libs',
    'dexen.system',
    'dexen.system.client',
    'dexen.system.common',
    'dexen.system.node',
    'dexen.system.node.master',
    'dexen.system.node.master.mgrs',
    'dexen.system.node.slave',
    'dexen.system.registry',
    'dexen.system.server'
]

#===============================================================================
# 
#===============================================================================
setup(
    name="dexen", 
    version="0.3.2", 
    license="GPL", 
    author="Patrick Janssen, Cihat Basol",
    description="Distributed Execution Environment",
    url="http://code.google.com/p/dexen",
    download_url="http://code.google.com/p/dexen/downloads/list",
    platforms="any",
    author_email="cihatbasol@gmail.com, patrick.ht.janssen@gmail.com",
    packages=_dexen_pkgs,
    data_files=[("", ["README.txt"])], # This is dummy, to be overriden. 
                                       # Needed to trigger the command.
    scripts=_scripts,
    cmdclass=_cmdclass
)

